﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace RestorationIce
{
    partial class RestoIce
    {
        #region Spells

        private bool ShouldAoe(int how_many,int Min_Health)
        { 
            int counter;
            counter = 0;
            foreach (WoWPlayer p in ObjectManager.GetObjectsOfType<WoWPlayer>(true, true))
            {
                if (unitcheck(p) == 1 && p.Distance < 40 && p.HealthPercent < Min_Health && p.IsInMyPartyOrRaid)
                {
                    counter++;
                }
            }
        if (counter >= how_many) {return true;}
        else {return false;}
         
        }
        private bool ShouldWildGrowth(int how_many)
        {
            int counter;
            counter = 0;
            if (!CanCast("Wild Growth"))
            {
                return false;
            }
            if (InRaid())
            {
                foreach (WoWPlayer p in Me.RaidMembers)
                {
                    if (p.HealthPercent < 90 && (Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 30f) > 2))
                    {
                        counter = Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 30f);
                    }
                }
            }
            else
            {
                foreach (WoWPlayer p in Me.PartyMembers)
                {
                    if (p.HealthPercent < 90 && (Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 30f) > 2))
                    {
                        counter = counter = Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 30f);
                    }
                }
                if (Me.HealthPercent < 90 && (Clusters.GetClusterCount(Me, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 30f) > 2)) { counter = Clusters.GetClusterCount(Me, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 30f); }
            }
            //slog(Color.DarkRed,"there are {0} injuried unit in 20 yard", counter);
            if (counter >= how_many)
            {
                slog(Color.DarkRed, "Wild Growth: there are {0} injuried unit in a 30 yard Radius of each other", counter);
                /*
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.Name, Round(Me.Distance), Round(Me.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember1.Name, Round(Me.PartyMember1.Distance), Round(Me.PartyMember1.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember2.Name, Round(Me.PartyMember2.Distance), Round(Me.PartyMember2.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember3.Name, Round(Me.PartyMember3.Distance), Round(Me.PartyMember3.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember4.Name, Round(Me.PartyMember4.Distance), Round(Me.PartyMember4.HealthPercent));
                */
                return true;
            }
            return false;
        }
        private bool ShouldEfflorescence(int how_many)
        {
            int counter;
            counter = 0;
            if (!CanCast("Swiftmend"))
            {
                return false;
            }
            if (InRaid())
            {
                foreach (WoWPlayer p in Me.RaidMembers)
                {
                    if (p.HealthPercent < 90 && (Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 8f) > 2))
                    {
                        counter = Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 8f);
                    }
                }
            }
            else
            {
                foreach (WoWPlayer p in Me.PartyMembers)
                {
                    if (p.HealthPercent < 90 && (Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 8f) > 2))
                    {
                        counter = counter = Clusters.GetClusterCount(p, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 8f);
                    }
                }
                if (Me.HealthPercent < 90 && (Clusters.GetClusterCount(Me, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 8f) > 2)) { counter = Clusters.GetClusterCount(Me, Unit.NearbyFriendlyPlayers, ClusterType.Radius, 8f); }
            }
            //slog(Color.DarkRed,"there are {0} injuried unit in 20 yard", counter);
            if (counter >= how_many)
            {
                slog(Color.DarkRed, "Efflorescence: there are {0} injuried unit in a 8 yard Radius of each other", counter);
                /*
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.Name, Round(Me.Distance), Round(Me.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember1.Name, Round(Me.PartyMember1.Distance), Round(Me.PartyMember1.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember2.Name, Round(Me.PartyMember2.Distance), Round(Me.PartyMember2.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember3.Name, Round(Me.PartyMember3.Distance), Round(Me.PartyMember3.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember4.Name, Round(Me.PartyMember4.Distance), Round(Me.PartyMember4.HealthPercent));
                */
                return true;
            }
            return false;
        }
        private bool ShouldTranquility(int how_many)
        {
            int counter;
            counter = 0;
            if (!CanCast("Tranquility"))
            {
                return false;
            }
            if (InRaid())
            {
                foreach (WoWPlayer p in Me.RaidMembers)
                {
                    if (p.Distance < 40 && p.HealthPercent < 60 && !p.Dead && !p.IsGhost)
                    {
                        counter++;
                    }
                }
            }
            else
            {
                foreach (WoWPlayer p in Me.PartyMembers)
                {
                    if (p.Distance < 40 && p.HealthPercent < 60 && !p.Dead && !p.IsGhost)
                    {
                        counter++;
                    }
                }
                if (Me.HealthPercent < 60) { counter++; }
            }
            //slog(Color.DarkRed,"there are {0} injuried unit in 20 yard", counter);
            if (counter >= how_many)
            {
                slog(Color.DarkRed, "Tranquility: there are {0} injuried unit in 40 yard", counter);
                /*
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.Name, Round(Me.Distance), Round(Me.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember1.Name, Round(Me.PartyMember1.Distance), Round(Me.PartyMember1.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember2.Name, Round(Me.PartyMember2.Distance), Round(Me.PartyMember2.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember3.Name, Round(Me.PartyMember3.Distance), Round(Me.PartyMember3.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember4.Name, Round(Me.PartyMember4.Distance), Round(Me.PartyMember4.HealthPercent));
                */
                return true;
            }
            return false;
        }
        private bool ShouldTree(int how_many)
        {
            int counter;
            counter = 0;
            if (!CanCast("Tree of Life"))
            {
                return false;
            }
            if (InRaid())
            {
                foreach (WoWPlayer p in Me.RaidMembers)
                {
                    if (p.Distance < 40 && p.HealthPercent < 45 && !p.Dead && !p.IsGhost)
                    {
                        counter++;
                    }
                }
            }
            else
            {
                foreach (WoWPlayer p in Me.PartyMembers)
                {
                    if (p.Distance < 40 && p.HealthPercent < 45 && !p.Dead && !p.IsGhost)
                    {
                        counter++;
                    }
                }
                if (Me.HealthPercent < 45) { counter++; }
            }
            //slog(Color.DarkRed,"there are {0} injuried unit in 20 yard", counter);
            if (counter >= how_many)
            {
                slog(Color.DarkRed, "Tree of Life: there are {0} injuried unit in 40 yard", counter);
                /*
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.Name, Round(Me.Distance), Round(Me.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember1.Name, Round(Me.PartyMember1.Distance), Round(Me.PartyMember1.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember2.Name, Round(Me.PartyMember2.Distance), Round(Me.PartyMember2.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember3.Name, Round(Me.PartyMember3.Distance), Round(Me.PartyMember3.HealthPercent));
                slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember4.Name, Round(Me.PartyMember4.Distance), Round(Me.PartyMember4.HealthPercent));
                */
                return true;
            }
            return false;
        }
        private bool ShouldRebirth()
        {            
            int counter = 0;
            double castit = 0;
            double varcalc = 0;
            if (!CanCast("Rebirth"))
            {                
                return false;
            }
            else
            {
                if (InRaid())
                {
                    foreach (WoWPlayer p in Me.RaidMembers)
                    {
                        if (p.Distance < 40 && p.HealthPercent > 45 && !p.Dead && !p.IsGhost)
                        {
                            counter++;                           
                        }
                    }
                }
                else
                {
                    foreach (WoWPlayer p in Me.PartyMembers)
                    {
                        if (p.Distance < 40 && p.HealthPercent > 45 && !p.Dead && !p.IsGhost)
                        {
                            counter++;                            
                        }
                    }
                    if (Me.HealthPercent > 45) { counter++; }                    
                }
                //slog(Color.DarkRed,"there are {0} injuried unit in 20 yard", counter);
                if (Enemy.HealthPercent <= 30)
                { varcalc = (Enemy.HealthPercent * 2 + 10) / 100; }
                else if (Enemy.HealthPercent > 30 && Enemy.HealthPercent <= 37.5)
                { varcalc = (Enemy.HealthPercent * 1.7 + 10) / 100; }
                else if (Enemy.HealthPercent > 75)
                { varcalc = (Enemy.HealthPercent + 10) / 100; }                
                if (Me.IsInRaid) { castit = Me.RaidMembers.Count * varcalc; }
                if (Me.IsInParty) { castit = Me.PartyMembers.Count * varcalc; }               
                if (counter >= castit)
                {
                    slog(Color.DarkRed, "Rebirth: there are {0} injuried unit in 40 yard", counter);
                    /*
                    slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.Name, Round(Me.Distance), Round(Me.HealthPercent));
                    slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember1.Name, Round(Me.PartyMember1.Distance), Round(Me.PartyMember1.HealthPercent));
                    slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember2.Name, Round(Me.PartyMember2.Distance), Round(Me.PartyMember2.HealthPercent));
                    slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember3.Name, Round(Me.PartyMember3.Distance), Round(Me.PartyMember3.HealthPercent));
                    slog(Color.DarkRed, "Player {0} discance {1} life {2} %", Me.PartyMember4.Name, Round(Me.PartyMember4.Distance), Round(Me.PartyMember4.HealthPercent));
                    */                   
                    return true;
                }
            }           
            return false;

        }
        private bool Resurrecting()
        {
            foreach (WoWPlayer p in GetResurrectTargets())
            {
                slog(Color.DarkRed, "I need to ress {0}", privacyname(p));
                if (Blacklist.Contains(p.Guid, true))
                {
                    continue;
                }
                else
                {
                    if (p.Distance > 40 || !p.InLineOfSight)
                    {
                        //MoveTo(p);
                        return true;
                    }
                    else if (CanCast("Revive", p) && castaress("Revive", p, 40, "Buff", "Ressing"))
                    {
                        Blacklist.Add(p, new TimeSpan(0, 0, 15));
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;
        }
        private bool NeedsRemoveCorruption(WoWPlayer p)
        {
            int dispelme;
            bool touchme;
            dispelme = 0;
            touchme = true;
            foreach (WoWAura a in p.ActiveAuras.Values)
            {
                if ((a.IsHarmful) && (!p.ActiveAuras.ContainsKey("Blackout")) && (!p.ActiveAuras.ContainsKey("Toxic Torment")) && (!p.ActiveAuras.ContainsKey("Frostburn Formula")) && (!p.ActiveAuras.ContainsKey("Burning Blood")) && (!p.ActiveAuras.ContainsKey("Unstable Affliction")))
                {
                    WoWDispelType t = a.Spell.DispelType;
                    if (t == WoWDispelType.Curse || t == WoWDispelType.Magic || t == WoWDispelType.Poison)
                    {
                        dispelme += 1;
                        // return true;
                    }
                }
                else if ((p.ActiveAuras.ContainsKey("Blackout")) || (p.ActiveAuras.ContainsKey("Toxic Torment")) || (p.ActiveAuras.ContainsKey("Frostburn Formula")) | (p.ActiveAuras.ContainsKey("Burning Blood")) || (p.ActiveAuras.ContainsKey("Unstable Affliction")))
                {
                    touchme = false;
                }
            }
            if ((dispelme > 0) && (touchme)) { return true; } else { return false; }
        }        
        private void AutoAttack()
        {
            if (!Me.IsAutoAttacking)
            {
                Lua.DoString("StartAttack()");
            }

        }
        #endregion
    }
}
